﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Game
{


    class SlimeGame
    {
        Map _map;
        Player[] _players;
        int _TurnToWho;
        const int PlayerNum =2;
        public SlimeGame()
        {
            _TurnToWho = 0;            
            _map = new Map();
            _players = new Player[PlayerNum];
            _players[0] = new Player('a', 6, 4, "Player 1");
            _players[1] = new Player('A', 29, 4, "Player 2");           
            _map.Update(_players[0], _players[1]);
            Turn(_TurnToWho);
        }

        public SlimeGame(SlimeGame other)
        {
            _players = new Player[PlayerNum];
            _map = new Map(other._map);
            for (int i = 0; i < PlayerNum; i++) {
                _players[i] = new Player(other._players[i]);
            }
            _map.Update(_players[0], _players[1]);
            _TurnToWho = other._TurnToWho;            
        }
        public void Display()
        {
            _map.Update(_players[0], _players[1]);
            _map.Display();
            _players[0].Display('a');
            _players[1].Display('A');
        }
        public int TurnToWho {
            get { return _TurnToWho; }
        }
        public UI GetUI(){
            return new UI(this,ref _map,ref _players);
        
        }
        public void Update() {
            _map.Update(_players[0], _players[1]);
        }
        public void Move(int id, Position Goal,char side)
        {
            Slime S = GetSlime(side, id);
            if (_map.IsSpace(Goal))
            {
                if (_TurnToWho == 0 && char.IsLower(side))
                {
                    if (_map.CanGOp1(S.Pos, S.MovDis, Goal) && S.CanMove)
                    {
                        S.Move(Goal);
                    }                   
                }
                else if (_TurnToWho == 1 && char.IsUpper(side))
                {
                    if (_map.CanGOp2(S.Pos, S.MovDis, Goal))
                    {
                        S.Move(Goal);
                    }
                  
                }
            }
            Update();
        }

        Slime GetSlime(char side, int id) {
            if (char.IsLower(side))
            {
                return _players[0].getSlime(id);
            }
            else {
                return _players[1].getSlime(id);
            }
        }
        public void Atk(Slime my, Slime you)
        {

            my.Attack(you);
            _players[0].Update();
            _players[1].Update();

        }

        public void NoshowAtk(Slime my, Slime you)
        {

            my.MaAtk(you);
            _players[0].Update();
            _players[1].Update();

        }

        public void Turn(int turn)
        {
            _players[1 ^ turn].TurnOff();
            _players[turn].Turn();
        }

        public void State(Slime S)
        {
            S.Status();
            Console.Read();
        }


        public void Born(char cat, int x, int y)
        {
            Position position = new Position(x, y);
            Born(cat, position);
        }
        public void Born(char cat, Position P)
        {

            if (_map.IsSpace(P.X, P.Y))
            {
                if (_TurnToWho == 0)
                {
                    _players[0].Born(cat, P);
                }
                else if (_TurnToWho == 1)
                {
                    _players[1].Born(cat, P);
                }
            }
            Update();
        }
   
        public void Rest(char ID)
        {

            if (_TurnToWho == 0)
            {
                int id = ID - 'a';
                _players[0].Slimes[id].Stay();
            }
            else if (_TurnToWho == 1)
            {
                int id = ID - 'A';
                _players[1].Slimes[id].Stay();
            }
        }

        public void Rest()
        {

            if (_TurnToWho == 0)
            {
                for (int i = 0; i < _players[0].Slimes.Count; i++)
                {
                    _players[0].Slimes[i].Stay();
                }
            }
            else if (_TurnToWho == 1)
            {
                for (int i = 0; i < _players[1].Slimes.Count; i++)
                {
                    _players[1].Slimes[i].Stay();
                }
            }
        }
        
       




        public int Over()
        {
            if (_players[0].Slimes[0].IsDead())
                return 1;
            else if (_players[1].Slimes[0].IsDead())
                return 2;

            return 0;
        }
        public void Next() {
            Rest();
            

            ++_TurnToWho;
            if (_TurnToWho >= PlayerNum) {
                _TurnToWho -= PlayerNum;
            }
            Turn(_TurnToWho);
        }
    
    }

}

